package homework2

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.graphx.{Edge, EdgeTriplet, Graph}
import org.apache.spark.rdd.RDD

object GraphXDemo {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Graphx").setMaster("local[*]")

    val sc = new SparkContext(conf)

    sc.setLogLevel("warn")

    val vertexArray = Array(
      (1L, ("SFO", 1)),
      (2L, ("ORD", 1)),
      (3L, ("DFW", 1))
    )

    val vertexRDD = sc.makeRDD(vertexArray)

    val edgeArray = Array(
      Edge(1L, 2L, 1800),
      Edge(2L, 3L, 800),
      Edge(3L, 1L, 1400)
    )

    val edgeRDD = sc.makeRDD(edgeArray)

    val graph = Graph(vertexRDD, edgeRDD)

    //1、求所有的顶点
    val vertices = graph.vertices
    vertices.foreach(println)

    //2、求所有的边
    val edges = graph.edges
    edges.foreach(println)

    //3、求所有的triplets
    val triplets = graph.triplets
    triplets.foreach(println)

    //4.求顶点数
    val verticesCount = vertices.count()
    println(verticesCount)

    //5.求边数
    val edgesCount = edges.count()
    println(edgesCount)

    //6.求机场距离大于1000的有几个，有哪些按所有机场之间的距离排序（降序），输出结果
    val result: RDD[EdgeTriplet[(String, Int), Int]] = triplets.filter(t => t.attr > 1000).sortBy(t => t.attr)

    result.foreach(t => println(t.srcAttr._1, t.dstAttr._1, t.attr))
  }


}
